﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GmailReader.UI
{
    public partial class FormMain : Form
    {
        private BindingSource bindingSource = new BindingSource();

        public FormMain()
        {
            InitializeComponent();

            this.bindingSource.DataSource = new List<Account>();
            this.dataGrid.AutoGenerateColumns = false;
            this.dataGrid.DataSource = bindingSource;

            this.timer.Start();
        }

        private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            this.lblStatus.Text = "Checking...";

            foreach (Account account in this.bindingSource.DataSource as IList<Account>)
            {
                account.Update();
            }

            this.lblStatus.Text = "Last update at " + DateTime.Now.ToShortTimeString();
        }

        private void TimerTick(object sender, EventArgs e)
        {
            if (this.backgroundWorker.IsBusy)
                return;

            this.backgroundWorker.RunWorkerAsync();
        }

        private void AddAccountClick(object sender, EventArgs e)
        {
            FormAdd form = new FormAdd();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            this.bindingSource.Add(new Account(form.Email, form.Password));
        }

        private void ExitClick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
